package com.hudongwx.shansun.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.hudongwx.shansun.R;
import com.hudongwx.shansun.fragment.Fragment1;
import com.hudongwx.shansun.fragment.Fragment2;
import com.hudongwx.shansun.fragment.Fragment3;
import com.hudongwx.shansun.fragment.Fragment4;
import com.hudongwx.shansun.fragment.Fragment5;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
    @ViewInject(android.R.id.tabhost)
    private FragmentTabHost fragmentTabHost;
    private String tab = "home";
    private Handler handler;
    private boolean flag=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        fragmentTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        TabHost.TabSpec tabHome = fragmentTabHost.newTabSpec("home").setIndicator(inintView(getResources().getString(R.string.homeTab), R.drawable.selector_1));
        TabHost.TabSpec tabHot = fragmentTabHost.newTabSpec("hot").setIndicator(inintView(getResources().getString(R.string.hot), R.drawable.selector_2));
        TabHost.TabSpec tabGood = fragmentTabHost.newTabSpec("good").setIndicator(inintView(getResources().getString(R.string.good), R.drawable.selector_3));
        TabHost.TabSpec tabBuy = fragmentTabHost.newTabSpec("buy").setIndicator(inintView(getResources().getString(R.string.buy), R.drawable.selector_4));
        TabHost.TabSpec tabMy = fragmentTabHost.newTabSpec("my").setIndicator(inintView(getResources().getString(R.string.my), R.drawable.selector_5));
        fragmentTabHost.addTab(tabHome, Fragment1.class, null);
        fragmentTabHost.addTab(tabHot, Fragment3.class, null);
        fragmentTabHost.addTab(tabGood, Fragment2.class, null);
        fragmentTabHost.addTab(tabBuy, Fragment4.class, null);
        fragmentTabHost.addTab(tabMy, Fragment5.class, null);
        fragmentTabHost.getTabWidget().setDividerDrawable(null);
        handler = new Handler();
        fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                if (!"buy".equals(tabId)) {
                    tab = tabId;
                } else {
                    fragmentTabHost.setCurrentTabByTag(tab);
                    startActivity(new Intent(MainActivity.this, LoginActivity.class));

                }
            }
        });

    }


    private View inintView(String name, int src) {
        View view = getLayoutInflater().inflate(R.layout.init_view, null);
        ImageView iv = (ImageView) view.findViewById(R.id.init_view_iv);
        iv.setImageResource(src);
        TextView tv = (TextView) view.findViewById(R.id.init_view_tv);
        tv.setText(name);
        return view;

    }

    @Override
    public void onBackPressed() {

        if (flag) {
            Toast.makeText(this, "再次点击退出", Toast.LENGTH_SHORT).show();
            flag = false;
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    flag = true;
                }
            }, 1000);
        } else {
            super.onBackPressed();
        }

    }
}
